home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / setpgms.zip / ENV_SET.ASM next >
Assembly Source File  |  1987-01-15  |  27KB  |  702 lines

  1.                  PAGE   60,132 
  2.  
  3.                  ;Usage is: call envsub  ds:si -> length,string
  4.  
  5.                  ;      length is 1 byte long, <128
  6.                  ;         if high bit on, primary environment is set.
  7.                  ;      string is of form: name=value
  8.  
  9.                  ;   Copyright 1987, A. B. Krueger GPW MI 48236
  10.                  ;   All rights reserved. Contact "ARNY KRUEGER"
  11.                  ;   at the EXEC-PC BBS (414-964-5160) for permission 
  12.                  ;   to use commercially.
  13.                  ;
  14.  
  15.                  ;Clone of SET command that demonstrates updating
  16.                  ;      the environment string.
  17.                  ;If there is no secondary command processor, the
  18.                  ;      global environment is updated
  19.                  ;If there is a secondary command processor, then
  20.                  ;      its enviroment is updated
  21.  
  22. sb               segment at 0      ;equates storage blocks and psp's
  23.  
  24. sb_kind          db     ' '        ;type of storage block: 'M' or 'Z'
  25. sb_psp           dw     ?          ;psp segment address
  26. sb_length        dw     ?          ;sb length in paragraphs
  27. sb_head_length   equ    10h        ;length of sb header
  28.  
  29.                  org    0h         ;program segement prefix equates
  30. psp_ret_int      dw     ?          ;int 20h
  31.                  org    2Ch
  32. psp_env          dw     ?          ;segment address of environment
  33.                  org    50h
  34. psp_dos_function dw     ?          ;address of function dispatcher
  35.                  org    80h
  36. psp_parm_string  db     ?          ;1 byte length plus parm string
  37.  
  38. psp_length       equ    100h
  39.  
  40. sb               ends
  41.  
  42. code_seg         segment para public
  43.                  assume cs:code_seg,ds:code_seg,es:sb
  44.                  public env_set
  45.                        
  46.                  ;local  data
  47.  
  48. cr               equ    13
  49. lf               equ    10
  50.  
  51. sb_count         dw    0            ;count of sb's encountered
  52. sb_shell         dw    0            ;segment address of shell  sb
  53. sb_shell_env     dw    0            ;segment address of global env sb
  54. sb_secondary     dw    0            ;segment address of secondary command.com
  55. sb_secondary_env dw    0            ;segment address of secondary command env
  56.  
  57. fatal_msg       equ    80h 
  58. error_msg       equ    40h 
  59. info_msg        equ    20h
  60. msg_flag        db     fatal_msg+error_msg
  61.                 db     'Copyright 1987, A. B. Krueger GPW MI 48236'
  62. secondary_msg   db     info_msg,'Secondary '
  63. command_found   db     info_msg,'COMMAND.COM found',cr,lf,'$'
  64. bad_dos_msg     db     fatal_msg,'Must be running under DOS 2.0 or above',cr,lf,'$'
  65. bad_sb_msg      db     fatal_msg,'Bad storage block',cr,lf,'$'
  66. bad_env_msg     db     error_msg,'Bad environment block',cr,lf,'$'
  67. command_lost    db     error_msg,'Shell never found',cr,lf,'$'
  68. addbadmsg       db     error_msg,'Environment corrupt',cr,lf,'$'
  69. addmsg          db     info_msg,'Addition requested',cr,lf,'$'
  70. removemsg       db     info_msg,'Removal requested',cr,lf,'$'
  71. env_set_nospace db     error_msg,'No space in environment string',cr,lf,'$'
  72. env_set_syntax  db     error_msg,'Set string syntax error',cr,lf,'$'
  73.  
  74. type_string     proc   near          ;type message at offset in dx
  75.                 push   ax            ;save registers
  76.                 push   cx
  77.                 push   dx
  78.                 push   si
  79.  
  80.                 mov    si,dx         ;get message level
  81.                 lodsb 
  82.                 and    al,msg_flag   ;compare to what sells
  83.                 jz     type_ret      ;if not on list, send to bit bucket
  84.  
  85.                 mov    dx,si
  86.                 mov    ax,0900h
  87.                 int    21h
  88. type_ret:
  89.                 pop    si
  90.                 pop    dx
  91.                 pop    cx
  92.                 pop    ax
  93.                 ret
  94. type_string     endp
  95.  
  96. get_first_sb    proc   near       ;get first storage block, point es at it
  97.                 push   ax
  98.                 push   bx
  99.                 mov    ax,5200h       
  100.                 int    21h        ;es:bx points to memory block anchor+2
  101.                 dec    bx
  102.                 dec    bx
  103.                 mov    es,es:[bx] ;get first memory block address into es
  104.                 pop    bx
  105.                 pop    ax
  106.                 ret
  107. get_first_sb    endp
  108.  
  109. get_next_sb     proc   near
  110.                 push   ax
  111.                 mov    ax,es             ;get current paragraph
  112.                 add    ax,sb_length      ;add in number of paragraphs
  113.                 inc    ax                ;add 1 for header
  114.                 mov    es,ax             ;set new extra segment address
  115.                 pop    ax
  116.                 ret
  117. get_next_sb     endp
  118.  
  119.  
  120. find_secondary_env proc  near       ;find env sb's for current program sb
  121.                 push   ax           ;pointed to by es
  122.                 push   es
  123.                 mov    ax,es        ;get address of secondary cp's sb
  124.                 inc    ax           ;get its psp address
  125. find_secondary_env_loop:
  126.                 call   get_next_sb  ;get next sb
  127.                 cmp    ax,sb_psp    ;match secondary's psp?
  128.                 jne    find_secondary_env_next    ;if not, skip
  129.  
  130.                 mov    sb_secondary_env,es        ;otherwise, save
  131.                 jmp    find_secondary_env_exit    ;and check no further
  132.                                                   ;lest we trash a .BAT block
  133. find_secondary_env_next:
  134.                 cmp    sb_kind,'Z'                ;last block?
  135.                 jne    find_secondary_env_loop
  136.  
  137. find_secondary_env_exit:
  138.                 pop    es
  139.                 pop    ax
  140.                 ret
  141. find_secondary_env endp
  142.  
  143. command_test    proc   near         ;test program storage block at es:0
  144.                 push   ax
  145.                 push   bx
  146.                 push   cx
  147.                 push   dx
  148.                 push   ds
  149.                 push   es
  150.                 push   si
  151.  
  152.                 cmp    sb_count,2
  153.                 ja     command_second
  154.  
  155.                 mov    dx,offset command_found
  156.                 call   type_string
  157.                 mov    sb_shell,es
  158.                 jmp    command_test_good
  159.  
  160. command_second: 
  161.                 cmp    sb_shell,0                       ;did we find shell?
  162.                 je     command_first_bad                ;if not, error
  163.  
  164.                 cmp    word ptr es:psp_env+sb_head_length,0  ;check environment of program
  165.                 je     command_test_good                 ;if no environment, quit
  166.  
  167.                 push   sb_shell
  168.                 pop    ds                               ;ds points to shell
  169.                 mov    al,byte ptr es:sb_head_length+psp_length 
  170.                 cmp    al,0E9h                          ;a JMP?
  171.                 jne    command_test_good                ;if not, no harm done
  172.  
  173.                 cmp    al,byte ptr ds:sb_head_length+psp_length   ;check 1st instruction 
  174.                 jne    command_first_bad
  175.  
  176.                 mov    si,sb_head_length+psp_length               
  177.                 mov    di,sb_head_length+psp_length               
  178.                 mov    cx,10      ;look at 10 words of code
  179.                 repz   cmpsw
  180.                 clc
  181.                 jcxz   command_test_found   ;if they all match, fine
  182.  
  183.                 jmp    command_test_good    ;if not, no harm done
  184.  
  185. command_test_found:
  186.                 push   cs
  187.                 pop    ds
  188.                 mov    sb_secondary,es
  189.  
  190.                 mov    ax,es:psp_env+sb_head_length     ;get env address
  191.                 dec    ax                               ;back up over sb header
  192.                 mov    sb_secondary_env,ax              ;and save it
  193.  
  194.                 call   find_secondary_env               ;look for other env's
  195.  
  196.                 mov    dx,offset secondary_msg
  197.                 call   type_string
  198.                 jmp    command_test_good
  199.  
  200. command_first_bad:
  201.                 mov    dx,offset command_lost
  202.                 call   type_string
  203.                 stc
  204.                 jmp    command_test_end
  205.  
  206. command_test_good:
  207.